<?php
class Lua_Util_Array {
	/**
	 * Convert an array to an array with new index name
	 * @param array $convertRuleArr: the rules to convert
	 *        Ex:['the name' -> 'name', 'year old' -> 'age'] 
	 * @param array $soureArr: the source array to convert to new array
	 * 		  Ex: ['the name' -> 'Kevin', 'year old' -> '30']
	 * @return array converted array. 
	 * 		  Ex: with above sample data, the result will be: ['name' -> 'Kevin', 'age' -> '30']
	 */
	public static function convertArray(array $convertRuleArr, array $soureArr) {
		$desArr = array();
		foreach ($convertRuleArr as $sourceIndexName => $desIndexName) {
			if(isset($soureArr[$sourceIndexName])) {
				$desArr[$desIndexName] = $soureArr[$sourceIndexName];
			}
		}
		return $desArr;
	}
	
	/**
	 * Extract elements from an array.
	 * @param array $extractElements: define elements to extract elements from
	 *        Ex:['name', 'age'] 
	 * @param array $soureArr: the source array to extract elements
	 * 		  Ex: ['name' -> 'Kevin', 'age' -> '30', 'gender' -> 'male']
	 * @return array extracted array. 
	 * 		  Ex: with above sample data, the result will be: ['name' -> 'Kevin', 'age' -> '30']
	 */
	public static function extractArray(array $extractElements, array $soureArr) {
		$desArr = array();
		foreach ($extractElements as  $extractElement) {
			if(isset($soureArr[$extractElement])) {
				$desArr[$extractElement] = $soureArr[$extractElement];
			}
		}
		return $desArr;
	}
}